home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-03-13 | 1.5 KB | 102 lines | [TEXT/R*ch] |
- /*
- TickerThread.java - A timer thread class for running periodic tasks.
-
- Copyright (C) 1996-1997 by Michael J. Webb
- */
-
- // Imports
-
- import IdleTask;
-
- /** Timer thread class for running periodic tasks.
- */
- class TickerThread extends Thread
- {
- /* Construction/Destruction Methods. */
-
- /** Main constructor; takes an object with an idle task.
- */
- public TickerThread(IdleTask idleObject)
- {
- setDaemon(true);
- kickMe = idleObject;
- }
-
- /** Timed constructor; takes an object with a periodic task and a sleep time.
- */
- public TickerThread(IdleTask idleObject, int sleepTime)
- {
- this(idleObject);
- fSleepTime = sleepTime;
- }
-
- /* Public Methods. */
-
- /** The body of this thread.
- */
- public void run()
- {
- // Twiddle thumbs for a second so the UI can stabilize.
-
- try
- {
- sleep(1000);
- }
- catch (Throwable e)
- {
- }
-
- // Run the idle task of the dependent object and then take a nap.
-
- while (true)
- {
- try
- {
- synchronized (this)
- {
- kickMe.idle();
- }
-
- snooze();
- }
- catch (Throwable e)
- {
- System.err.println(e.toString());
- e.printStackTrace(System.err);
- }
- }
- }
-
- /* Private Members. */
-
- /** The dependent task.
- */
- private IdleTask kickMe;
-
- /** Task periodicity.
- */
- private int fSleepTime = 0;
-
- /* Private methods. */
-
- /** Yield time to other threads. If this is a periodic task, also
- sleep for a given amount of time.
- */
- private void snooze()
- {
- try
- {
- if (fSleepTime > 0)
- {
- sleep(fSleepTime);
- }
-
- yield();
- }
- catch (Throwable e)
- {
- }
- }
-
- }
-